/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package queryDB;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import queryDB.exceptions.NonexistentEntityException;

/**
 *
 * @author Paulo
 */
public class PlaceJpaController {

    public PlaceJpaController() {
        emf = Persistence.createEntityManagerFactory("RunAwayDBPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Place place) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(place);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Place place){
        EntityManager em = null;
        em = getEntityManager();

        em.getTransaction().begin();
        em.merge(place);

        em.getTransaction().commit();
        em.close();
    }

    public void destroy(Long id) {
        EntityManager em = null;
        em = getEntityManager();

        em.getTransaction().begin();
        Place place = em.getReference(Place.class, id);
        em.remove(place);

        em.getTransaction().commit();
        em.close();

//        EntityManager em = null;
//        try {
//            em = getEntityManager();
//            em.getTransaction().begin();
//            em.
//            Place place;
//            try {
//                place = em.getReference(Place.class, id);
//                place.getId();
//            } catch (EntityNotFoundException enfe) {
//                throw new NonexistentEntityException("The place with id " + id + " no longer exists.", enfe);
//            }
//            em.remove(place);
//            em.getTransaction().commit();
//        } finally {
//            if (em != null) {
//                em.close();
//            }
//        }
    }

    public List<Place> findPlaceEntities() {
        return findPlaceEntities(true, -1, -1);
    }

    public List<Place> findPlaceEntities(int maxResults, int firstResult) {
        return findPlaceEntities(false, maxResults, firstResult);
    }

    private List<Place> findPlaceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Place.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Place findPlace(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Place.class, id);
        } finally {
            em.close();
        }
    }

    public int getPlaceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Place> rt = cq.from(Place.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public long getID(String name) {
        long id = -1;
        EntityManager em = getEntityManager();
        List<Place> places = this.findPlaceEntities();
        try {
            for(int i=0; i<places.size(); i++)  {
                if (name.equals(places.get(i).getName()))
                    id = places.get(i).getId();
            }
            return id;
        }
        catch(Exception e){
            return -1;
        }
        finally {
            em.close();
        }
    }


    public long getNextID(){
        long id = -1;
        EntityManager em = getEntityManager();
        List<Place> places = this.findPlaceEntities();

        if (!places.isEmpty()){
            id = places.get(places.size()-1).getId();
        }

        return id;
    }
}
